//
// Created by 25429 on 2020/6/8.
//

#ifndef SIRIUS_STRATEGY_ENGINE_H
#define SIRIUS_STRATEGY_ENGINE_H

#include <utility>

#include "common_function.h"
#include "trade_engine.h"

class strategy_task {
public:
    explicit strategy_task(string name, trade_engine *trade) : m_name(std::move(name)), m_trade_engine(trade) {};

    // 初始化
    virtual void init() {
        m_log_title = m_name + "/" + m_stock_info.securityId + ": ";
    };

    virtual void on_md_l2_mkt(const SiriusMDRspMarketField &mkt) {};

    // 卖出价格计算
    int order_sell_price() {
        auto price = common_function::std_price_multiplication(m_last_mkt.last_price, PERCENT_98);
        return m_stock_info.lowerLimitPrice < price ? price : m_stock_info.lowerLimitPrice;
    }

    // 买入价格计算
    int order_buy_price() {
        auto price = common_function::std_price_multiplication(m_last_mkt.last_price, PERCENT_102);
        return m_stock_info.upperLimitPrice > price ? price : m_stock_info.upperLimitPrice;
    }

    // 买入委托
    bool order_buy() {
        if (PM_14_55 < m_last_mkt.time || AM_09_33 > m_last_mkt.time) {
            util::Singleton<my_log>::Instance()->add_log(
                    m_log_title + "incorrect trading time:" + to_string(m_last_mkt.time));
            return false;
        }
        if (m_target_volume <= 0) {
            util::Singleton<my_log>::Instance()->add_log(m_log_title + "order volume less than 0");
            m_phase = LIMIT_UP_PHASE::STOP;
            return false;
        }
        m_order_time = m_last_mkt.time;

        // 下单
        m_trade_engine->req_trade_order_insert(
                SiriusTradeReqOrderInsertField(m_stock_info.securityId, TRADE_DIRECTION_BUY, m_target_volume,
                                               order_buy_price(), m_system_order_insert_no, m_last_mkt.original_point));
        util::Singleton<my_log>::Instance()->add_log(m_log_title + "order buy");
        return true;
    };

    // 卖出委托
    bool order_sell() {
        if (AM_09_30 > m_last_mkt.time || PM_14_57 <= m_last_mkt.time) {
            util::Singleton<my_log>::Instance()->add_log(
                    m_log_title + "incorrect trading time:" + to_string(m_last_mkt.time));
            return false;
        }
        if (m_target_volume <= 0) {
            util::Singleton<my_log>::Instance()->add_log(m_log_title + "order volume less than 0");
            m_phase = LIMIT_UP_PHASE::STOP;
            return false;
        }
        m_order_time = m_last_mkt.time;

        // 下单
        m_trade_engine->req_trade_order_insert(
                SiriusTradeReqOrderInsertField(m_stock_info.securityId, TRADE_DIRECTION_SELL, m_target_volume,
                                               order_sell_price(), m_system_order_insert_no,
                                               m_last_mkt.original_point));
        util::Singleton<my_log>::Instance()->add_log(m_log_title + "order sell");
        return true;
    };

    // 卖出委托
    bool order_sell_fast() {
        if (AM_09_15 > m_last_mkt.time || PM_14_57 < m_last_mkt.time) {
            util::Singleton<my_log>::Instance()->add_log(
                    m_log_title + "incorrect trading time:" + to_string(m_last_mkt.time));
            return false;
        }
        if (m_target_volume <= 0) {
            util::Singleton<my_log>::Instance()->add_log(m_log_title + "order volume less than 0");
            m_phase = LIMIT_UP_PHASE::STOP;
            return false;
        }
        m_order_time = m_last_mkt.time;

        // 下单
        m_trade_engine->req_trade_order_insert(
                SiriusTradeReqOrderInsertField(m_stock_info.securityId, TRADE_DIRECTION_SELL, m_target_volume,
                                               m_stock_info.lowerLimitPrice, m_system_order_insert_no,
                                               m_last_mkt.original_point));
        util::Singleton<my_log>::Instance()->add_log(m_log_title + "order sell");
        return true;
    };

    // 发送撤单
    bool order_cancel() const {
        // 撤单
        m_trade_engine->req_trade_order_cancel(
                SiriusTradeReqOrderInsertField(m_system_order_insert_no, m_last_mkt.original_point));
        util::Singleton<my_log>::Instance()->add_log(m_log_title + "order cancel");
        return true;
    }

public:
    string m_log_title{};
    MdsStockStaticInfoT m_stock_info{};// 股票信息
    SiriusMDRspMarketField m_last_mkt;// 最新报价

    double m_shift_1_block_money = 0; // 往前1个tick封单金额
    double m_shift_1_total_deal = 0; // 往前1个tick成交金额

    int m_order_time{0};// 委托时间
    int m_target_volume{100};// 目标数量

    int m_delay = SECOND_10;

    int16 m_system_order_insert_no = common_function::uuid();// 专属策略任务识别码
    int16 m_phase{0};
    int16 m_task_type;
    trade_engine *m_trade_engine{nullptr};// 交易接口
    string m_name;
};

#endif //SIRIUS_STRATEGY_ENGINE_H
